package com.b2bpsssm.controller.capital;

import com.b2bpsssm.common.R;
import com.b2bpsssm.pojo.Other_Revenue;
import com.b2bpsssm.service.OtherRevenueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@Api(value = "其他收入数据测试",tags = "其他收入API")
public class OtherRevenueController {
    @Autowired
     private OtherRevenueService otherRevenueService;

        //      查询其他收入信息 有分页
        @RequestMapping(value = "/other_revenue/queryOtherRevenueList",method = {RequestMethod.GET},produces = "application/json;charset=utf-8")
        @ApiOperation(value = "其他收入信息 ",notes = " 查询其他收入信息 有分页",httpMethod = "get",response = R.class)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "page", value = "当前页码数", required = true),
                @ApiImplicitParam(name = "limit", value = "每页条数", required = true),
        })
        @ResponseBody
      public R queryOtherRevenueList(Integer page, Integer limit){
       // 调用Service层进行业务逻辑处理
        return otherRevenueService.queryOtherRevenueList(page,limit);
                }


    //    以收支类型关键字 时间范围 进行关键字查询
    @RequestMapping(value = "/other_revenue/selOtherRevenueLikeType",method = RequestMethod.GET)
    @ApiOperation(value = "其他收入信息 ",notes = " 以收支类型关键字 时间范围 进行关键字查询",httpMethod = "get",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码数", required = true),
            @ApiImplicitParam(name = "limit", value = "每页条数", required = true),
            @ApiImplicitParam(name = "other_revenue_creation_time1", value = "开始时间", required = true),
            @ApiImplicitParam(name = "other_revenue_creation_time2", value = "结束时间", required = true),
            @ApiImplicitParam(name = "other_revenue_payment_type", value = "其他收入（收支类型）", required = true),


    })
    @ResponseBody
    public R selOtherRevenueLikeType(Integer page, Integer limit, String other_revenue_creation_time1, String other_revenue_creation_time2, String other_revenue_payment_type) {

        R r = otherRevenueService.selOtherRevenueLikeType(page,limit,other_revenue_creation_time1,other_revenue_creation_time2,other_revenue_payment_type);
        return r;
    }

    //添加其他收入
    @RequestMapping(value ="/other_revenue/addOtherRevenue",method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "其他收入信息 ",notes = "添加其他收入",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "other_revenue_bills", value = "单据编号", required = true),
            @ApiImplicitParam(name = "other_revenue_payment_type", value = "收支类型", required = true),
            @ApiImplicitParam(name = "other_revenue_clearing_form", value = "结算方式", required = true),
            @ApiImplicitParam(name = "other_revenue_money", value = "金额", required = true),
            @ApiImplicitParam(name = "type_of_expense", value = "费用分类", required = true),
            @ApiImplicitParam(name = "other_revenue_creation_time", value = "创建时间", required = true),
            @ApiImplicitParam(name = "credit_note", value = "收款备注", required = false),
            @ApiImplicitParam(name = "other_revenue_accessory", value = "上传附件", required = false),
            @ApiImplicitParam(name = "shop_id", value = "门店ID", required = true),


    })
    @ResponseBody
    public R addOtherRevenue(@RequestBody Other_Revenue other_revenue) {

        return  otherRevenueService.addOtherRevenue(other_revenue);


    }
//    @RequestMapping(value = "/other_revenue/accessory",method = RequestMethod.POST)
//    @ResponseBody
//    public R updateOtherRevenueAccessory(@Validated Other_Revenue other_revenue, HttpServletRequest request) throws IOException {
//        R r = new R();
//        if (other_revenue.getOther_revenue_accessory().isEmpty()) {
//            System.out.println("上传失败");
//            r.setCode(500);
//            r.setMassage("上传文件不能为空--上传失败");
//        } else {
//            System.out.println("开始上传");
//            String uploadPath = request.getServletContext().getRealPath("") + File.separator + "upload" + File.separator;
//            File file = new File(uploadPath);
//            if(!file.exists()){
//                file.mkdirs();
//            }
//            MultipartFile multipartFile = other_revenue.getOther_revenue_accessory();
//            String fileName = multipartFile.getOriginalFilename();
//            File newFile = new File(uploadPath + fileName);
//            System.out.println("newFile=" + newFile);
//            FileCopyUtils.copy(multipartFile.getBytes(), newFile);
//            r.setCode(200);
//            r.setMassage("上传成功");
//        }
//        return r;
//    }
    //修改信息
    @RequestMapping(value = "/other_revenue/updateOtherRevenue",method = RequestMethod.POST)
    @ApiOperation(value = "其他收入信息 ",notes = " 修改信息",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "other_revenue_bills", value = "单据编号", required = true),
            @ApiImplicitParam(name = "other_revenue_payment_type", value = "收支类型", required = true),
            @ApiImplicitParam(name = "other_revenue_clearing_form", value = "结算方式", required = true),
            @ApiImplicitParam(name = "other_revenue_money", value = "金额", required = true),
            @ApiImplicitParam(name = "type_of_expense", value = "费用分类", required = true),
            @ApiImplicitParam(name = "other_revenue_creation_time", value = "创建时间", required = true),
            @ApiImplicitParam(name = "credit_note", value = "收款备注", required = false),
            @ApiImplicitParam(name = "other_revenue_accessory", value = "上传附件", required = false),
            @ApiImplicitParam(name = "shop_id", value = "门店ID", required = true),


    })
    @ResponseBody
    public R updateOtherRevenue(@RequestBody Other_Revenue other_revenue){

        return otherRevenueService.updateOtherRevenue(other_revenue);
    }

    // 删除信息
    @RequestMapping(value = "/other_revenue/delOtherRevenueByNumber",method = RequestMethod.POST)
    @ApiOperation(value = "其他收入信息 ",notes = "删除信息",httpMethod = "POST",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "other_revenue_bills", value = "单据编号", required = true),
    })
    @ResponseBody
    public R delOtherRevenueByNumber(Other_Revenue other_revenue_bills){

        return  otherRevenueService.delOtherRevenueByNumber(other_revenue_bills);
    }
    }

